package com.weilive.push.service.umeng;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.weilive.core.utils.DateUtil;
import com.weilive.core.utils.Utils;
import com.weilive.push.ServerContext;
import com.weilive.push.service.umeng.android.AndroidBroadcast;
import com.weilive.push.service.umeng.android.AndroidFilecast;
import com.weilive.push.service.umeng.android.AndroidListcast;
import com.weilive.push.service.umeng.android.AndroidUnicast;

public class UmengPuser {

	/**
	 * 单个广播
	 * 
	 * @param token
	 * @param msg
	 * @param userId
	 */
	public static void AndroidUnicast(String token, String msg, Long userId, int expireHour) {
		if (token == null || token.equals("")) {
			return;
		}
		try {
			// 单播
			AndroidUnicast unicast = new AndroidUnicast();
			unicast.setAppMasterSecret(ServerContext.getUmengAppMasterSecret());
			unicast.setPredefinedKeyValue("appkey", ServerContext.getUmengAppKey());
			unicast.setPredefinedKeyValue("timestamp", Integer.toString((int) (System.currentTimeMillis() / 1000)));
			// Set your device token
			unicast.setPredefinedKeyValue("device_tokens", token);
			unicast.setPredefinedKeyValue("ticker", "live");// 通知栏文字
			unicast.setPredefinedKeyValue("title", "live");
			unicast.setPredefinedKeyValue("text", msg);
			unicast.setPredefinedKeyValue("after_open", "go_app");// 打开应用
			unicast.setPredefinedKeyValue("display_type", "notification");
			// 正式/测试模式。测试模式下，只会将消息发给测试设备。
			unicast.setPredefinedKeyValue("production_mode", "true"); // 可选
			unicast.setPredefinedKeyValue("description", msg);

			if (expireHour > 0) {
				Date expireTime = DateUtil.addTime(DateUtil.HOUR, expireHour, new Date());
				unicast.setPredefinedKeyValue("expire_time", DateUtil.dateFormat(expireTime));
			}

			// Set customized fields
			unicast.setExtraField("userId", userId + "");
			unicast.setExtraField("messageType", "live");
			unicast.send();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 单个广播
	 * 
	 * @param token
	 * @param msg
	 * @param userId
	 */
	public static void AndroidUnicast(String token, String msg, Long userId) {
		AndroidUnicast(token, msg, userId, 0);
	}

	/**
	 * 多个广播
	 * 
	 * @param tokens
	 * @param msg
	 * @param userId
	 */
	public static void AndroidListcast(List<String> tokens, String msg, Long userId, int expireHour) {
		if (tokens == null || tokens.isEmpty()) {
			return;
		}
		try {
			// 单播
			AndroidListcast listcast = new AndroidListcast();
			listcast.setAppMasterSecret(ServerContext.getUmengAppMasterSecret());
			listcast.setPredefinedKeyValue("appkey", ServerContext.getUmengAppKey());
			listcast.setPredefinedKeyValue("timestamp", Integer.toString((int) (System.currentTimeMillis() / 1000)));
			// Set your device token
			listcast.setPredefinedKeyValue("device_tokens", Utils.listConvert2String(tokens, ","));
			listcast.setPredefinedKeyValue("ticker", "live");// 通知栏文字
			listcast.setPredefinedKeyValue("title", "live");
			listcast.setPredefinedKeyValue("text", msg);
			listcast.setPredefinedKeyValue("after_open", "go_app");// 打开应用
			listcast.setPredefinedKeyValue("display_type", "notification");
			// 正式/测试模式。测试模式下，只会将消息发给测试设备。
			listcast.setPredefinedKeyValue("production_mode", "true"); // 可选
			listcast.setPredefinedKeyValue("description", msg);

			if (expireHour > 0) {
				Date expireTime = DateUtil.addTime(DateUtil.HOUR, expireHour, new Date());
				listcast.setPredefinedKeyValue("expire_time", DateUtil.dateFormat(expireTime));
			}
			// Set customized fields
			listcast.setExtraField("userId", userId + "");
			listcast.setExtraField("messageType", "live");
			listcast.send();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 多个广播
	 * 
	 * @param tokens
	 * @param msg
	 * @param userId
	 */
	public static void AndroidListcast(List<String> tokens, String msg, Long userId) {
		AndroidListcast(tokens, msg, userId, 0);
	}

	/**
	 * 全广播
	 * 
	 * @param msg
	 * @param userId
	 */
	public static void AndroidBroadcast(String msg, Long userId, int expireHour) {
		try {
			// 单播
			AndroidBroadcast broadcast = new AndroidBroadcast();
			broadcast.setAppMasterSecret(ServerContext.getUmengAppMasterSecret());
			broadcast.setPredefinedKeyValue("appkey", ServerContext.getUmengAppKey());
			broadcast.setPredefinedKeyValue("timestamp", Integer.toString((int) (System.currentTimeMillis() / 1000)));
			// Set your device token
			broadcast.setPredefinedKeyValue("ticker", "live");// 通知栏文字
			broadcast.setPredefinedKeyValue("title", "live");
			broadcast.setPredefinedKeyValue("text", msg);
			broadcast.setPredefinedKeyValue("after_open", "go_app");// 打开应用
			broadcast.setPredefinedKeyValue("display_type", "notification");
			// 格式 YYYY-MM-DD HH:mm:ss
			// 正式/测试模式。测试模式下，只会将消息发给测试设备。
			broadcast.setPredefinedKeyValue("production_mode", "true"); // 可选
			broadcast.setPredefinedKeyValue("description", msg);
			if (expireHour > 0) {
				Date expireTime = DateUtil.addTime(DateUtil.HOUR, expireHour, new Date());
				broadcast.setPredefinedKeyValue("expire_time", DateUtil.dateFormat(expireTime));
			}
			// Set customized fields
			broadcast.setExtraField("userId", userId + "");
			broadcast.setExtraField("messageType", "live");
			broadcast.send();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 全广播
	 * 
	 * @param msg
	 * @param userId
	 */
	public static void AndroidBroadcast(String msg, Long userId) {
		AndroidBroadcast(msg, userId, 0);
	}

	/**
	 * 以文件的形式广播
	 * 
	 * @param contents
	 *            token1\ntoken2\ntoken3
	 * @param msg
	 * @param userId
	 */
	public static void AndroidFilecast(String contents, String msg, Long userId, int expireHour) {
		if (contents == null || contents.trim().equals("")) {
			return;
		}
		try {
			// 单播
			AndroidFilecast filecast = new AndroidFilecast();
			filecast.setAppMasterSecret(ServerContext.getUmengAppMasterSecret());
			filecast.setPredefinedKeyValue("appkey", ServerContext.getUmengAppKey());
			filecast.setPredefinedKeyValue("timestamp", Integer.toString((int) (System.currentTimeMillis() / 1000)));
			// Set your device token
			// token1\ntoken2
			filecast.uploadContents(contents);
			filecast.setPredefinedKeyValue("ticker", "live");// 通知栏文字
			filecast.setPredefinedKeyValue("title", "live");
			filecast.setPredefinedKeyValue("text", msg);
			filecast.setPredefinedKeyValue("after_open", "go_app");// 打开应用
			filecast.setPredefinedKeyValue("display_type", "notification");
			// 正式/测试模式。测试模式下，只会将消息发给测试设备。
			filecast.setPredefinedKeyValue("production_mode", "true"); // 可选
			filecast.setPredefinedKeyValue("description", msg);
			if (expireHour > 0) {
				Date expireTime = DateUtil.addTime(DateUtil.HOUR, expireHour, new Date());

				filecast.setPredefinedKeyValue("expire_time", DateUtil.dateFormat(expireTime));
			}
			// Set customized fields
			filecast.setExtraField("userId", userId + "");
			filecast.setExtraField("messageType", "live");

			filecast.send();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 以文件的形式广播
	 * 
	 * @param contents
	 *            token1\ntoken2\ntoken3
	 * @param msg
	 * @param userId
	 */
	public static void AndroidFilecast(String contents, String msg, Long userId) {
		AndroidFilecast(contents, msg, userId, 0);
	}
}
